import java.io.File;
import java.io.IOException;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

// Diese Klasse wurde anhand eines Bsp. implementiert/umgeschrieben (Quelle: http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/)
public class xml2graph_parser {
	private SAXParserFactory factory;
	private SAXParser saxParser;
	private DefaultHandler handler;
	private static CyclicBarrier barrier;
	
	private static Graph graph;
    
    public xml2graph_parser(Graph gr, CyclicBarrier parserBarrier) {
        try {
        	factory	= SAXParserFactory.newInstance();
			saxParser = factory.newSAXParser();
			this.graph= gr;
			barrier= parserBarrier;
			handler = new DefaultHandler() {
				 
				 int aktId=0;
				 int aktSrcid=0;
			     boolean bId = false;
			     boolean bCap = false;
			     boolean bSrcid = false;
			     boolean bDstid = false;
				private Graph gr = graph;
			 
			     public void startElement(String uri, String localName,
			        String qName, Attributes attributes)
			        throws SAXException {
			    	if (qName.equalsIgnoreCase("edge")){
			    		try {
							barrier.await();								//wait until all thread have finished nodes 
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (BrokenBarrierException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
			    	}
			 
			        if (qName.equalsIgnoreCase("ID")) {
			           bId = true;
			        }
			 
			        if (qName.equalsIgnoreCase("CAPACITY")) {
			           bCap = true;
			        }
			 
			        if (qName.equalsIgnoreCase("srcid")) {
			           bSrcid = true;
			        }
			 
			        if (qName.equalsIgnoreCase("dstid")) {
			           bDstid = true;
			        }
			 
			     }
			 
			     public void endElement(String uri, String localName,
			          String qName)
			          throws SAXException {}
			 
			     public void characters(char ch[], int start, int length)
			         throws SAXException {
			        
			        
					if (bId) {
			        	aktId = Integer.valueOf(new String(ch, start, length));
			        	 
			            bId = false;
			          }
			 
			          if (bCap) {
			        	  int cap = Integer.valueOf(new String(ch, start, length));
			        	  gr.addKn(new Knoten(aktId, cap));
//			        	  put(aktId, new Knoten(aktId, cap));
			              bCap = false;
			           }
			 
			          if (bSrcid) {
				          aktSrcid = Integer.valueOf(new String(ch, start, length));

			              bSrcid = false;
			           }
			 
			          if (bDstid) {
			        	  int Dtsid = Integer.valueOf(new String(ch, start, length));
			        	 this.gr.getKn(Dtsid).addPre(gr.getKn(aktSrcid));
			        	 this.gr.getKn(aktSrcid).addNf(gr.getKn(Dtsid));
//			        	  ret.get(Dtsid).addPre(ret.get(aktSrcid));
			              bDstid = false;
			           }
			 
			        }
			 
			      };
        }catch (Exception e) {} // should not happen
		
	}
    public void createGraph(File f){
    	 try {
			saxParser.parse(f, handler);
		} catch (SAXException e) {} catch (IOException e) {} // should not happen
    }
}
